using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vms.Web.Models;
using System.IO;
using Vms.Web.Lib;
using System.Drawing;

namespace Vms.Web.Areas.Lib.Controllers
{   
    public class ImgsController : Controller
    {
        private VmsWebContext context = new VmsWebContext();
        private string ImgRoot
        {
            get { return Path.Combine(Server.MapPath("~/Areas/Lib/Lib_Data/Imgs")); }
            
        }
        public ActionResult ImgUpload()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ImgUploadJson()
        {

            var statuses = new List<ViewDataUploadFilesResult>();
            // Loop through each file in the request
            for (int i = 0; i < HttpContext.Request.Files.Count; i++)
            {
                // Pointer to file
                var file = HttpContext.Request.Files[i];

                // Save info to DB
                LIB_IMG lib_img = new LIB_IMG();
                Image img = Image.FromStream(file.InputStream);
                lib_img.Name = Path.GetFileNameWithoutExtension(file.FileName);
                lib_img.Height = img.Height;
                lib_img.Width =  img.Width;
                lib_img.Format = file.ContentType;
                lib_img.ImgFileName = Path.GetFileName(file.FileName);
                context.LIB_IMGS.Add(lib_img);
                context.SaveChanges();

                // Save file to server
                var fullPath = Path.Combine(ImgRoot, Path.GetFileName(file.FileName)); //StorageRoot + ;
                file.SaveAs(fullPath);

                // Add to statuses
                statuses.Add(new ViewDataUploadFilesResult()
                {
                    name = lib_img.ImgFileName,
                    size = file.ContentLength,
                    type = file.ContentType,
                    url =  Url.Content("~/Areas/Lib/Lib_Data/Imgs/") + lib_img.ImgFileName,
                    delete_url = Url.Content("~/Lib/Imgs/DeleteImg/") + lib_img.Id,
                    edit_url = Url.Content("~/Lib/Imgs/Edit/") + lib_img.Id,
                    thumbnail_url = null,
                    delete_type = "GET",
                    width = img.Width,
                    height = img.Height,
                });
            }
            JsonResult result = Json(statuses);
            result.ContentType = "text/plain";
            return result;
        }

        //
        // GET: /LIB_IMG/

        public ViewResult Index(string searchString)
        {
            var lib_imgs = from m in context.LIB_IMGS
                           select m;

            if (!String.IsNullOrEmpty(searchString))
            {
                lib_imgs = lib_imgs.Where(s => s.Name.Contains(searchString));
            }

            return View(lib_imgs);
        }

        //
        // GET: /LIB_IMG/Details/5

        public ViewResult Details(int id)
        {
            LIB_IMG lib_img = context.LIB_IMGS.Single(x => x.Id == id);
            return View(lib_img);
        }

        //
        // GET: /LIB_IMG/Create

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int id)
        {
            LIB_IMG lib_img = context.LIB_IMGS.Single(x => x.Id == id);
            return View(lib_img);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            int id = int.Parse(collection["Lib_Img_Id"]);
            LIB_IMG lib_img = context.LIB_IMGS.Single(x => x.Id == id);
            lib_img.Name = collection["Name"];
            context.Entry(lib_img).State = EntityState.Modified;
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /LIB_IMG/Create

        // POST: /LIB_IMG/Create

        [AllowAnonymous]
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(LIB_IMG lib_img)
        {
            return View(lib_img);
        }

        //
        // GET: /LIB_IMG/Delete/5

        public ActionResult Delete(int id)
        {
            LIB_IMG lib_img = context.LIB_IMGS.Single(x => x.Id == id);
            return View(lib_img);
        }

        [HttpPost]
        public ActionResult DeleteImg(int id)
        {
            LIB_IMG lib_img = context.LIB_IMGS.Single(x => x.Id == id);
            // Delete file
            try
            {
                System.IO.File.Delete(Utility.PathLibContentImg + lib_img.ImgFileName);
            }
            catch (Exception e)
            {
                
            }
            context.LIB_IMGS.Remove(lib_img);
            context.SaveChanges();
            return Json(new {success=true});
        }

        //
        // POST: /LIB_IMG/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                context.Dispose();
            }
            base.Dispose(disposing);
        }
    }
    public class ViewDataUploadFilesResult
    {
        public string name { get; set; }
        public int size { get; set; }
        public string type { get; set; }
        public string url { get; set; }
        public string delete_url { get; set; }
        public string thumbnail_url { get; set; }
        public string delete_type { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string edit_url { get; set; }
    }
}